package com.labofjet.blogv3.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.labofjet.blogv3.facade.SearchFacade;
import com.labofjet.blogv3.model.Artical;

/**
 * 搜索功能的Controller
 * 
 * @author Jet
 * @version 2014.8
 * 
 */
@Controller
public class SearchController {

	@Autowired
	private SearchFacade searchFacade;

	/**
	 * 搜索界面Controller
	 * 
	 * @return
	 */
	@RequestMapping("/search")
	public String search(String keyword, int currentPage, Model m) {
		m.addAttribute("keyword", keyword);
		m.addAttribute("currentPage", currentPage - 1);
		return "search";
	}
	
	/**
	 * 搜索界面Controller
	 * 
	 * @return
	 */
	@RequestMapping("/search/{keyword}/{currentPage}")
	public String search2(@PathVariable String keyword, @PathVariable int currentPage, Model m) {
		m.addAttribute("keyword", keyword);
		m.addAttribute("currentPage", currentPage - 1);
		return "search";
	}

	/**
	 * 搜索
	 * 
	 * @return
	 */
	@RequestMapping("/search-list/{keyword}/{currentPage}")
	public String searchList(Model m, @PathVariable String keyword, @PathVariable int currentPage) {
		Page<Artical> articals = searchFacade.findArticals(keyword, currentPage);
		//System.out.println(articals.getSize()); 每页几个元素
		//System.out.println(articals.getNumber()); 当前是第几页
		//System.out.println(articals.getNumberOfElements()); 当前页有几个元素
		m.addAttribute("articals", articals);
		m.addAttribute("keyword", keyword);
		return "import/search-articals";
	}
}
